package com.cis467.mrball;


import com.cis467.mrball.backend.GameManagement;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Display;
import android.view.View;
import android.widget.Button;


public class MainMenu extends Activity {
	
	private boolean debug = false; // TODO remove this
	
	/**
	 * Called when the activity is first created.
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mainmenu_layout);
		
		Button tutorialButton = (Button) findViewById(R.id.tutorialButton);
		Button levelSelectButton = (Button) findViewById(R.id.levelSelectButton);
		Button highScoreButton = (Button) findViewById(R.id.highScoreButton);
		Button creditsButton = (Button) findViewById(R.id.creditsButton);
		Button exitButton = (Button) findViewById(R.id.exitButton);

		if (debug || GameManagement.nothingSaved() ){
			
			GameManagement.setupGame(this.getResources());
			
			//TODO start ScreenPrefService or a separate thread to determine dimensions
			// For now, figure it out in-line
			setScreenPreferences();
			
			// TODO this should probably launch an activity for result and then check that it actually completed
			// For now, just set default values (basic calibration currently available in game activity)
			setCalibrationPreferences();
		}
		
		//displays level menu
		tutorialButton.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				Intent intent = new Intent(v.getContext(), MrBall.class);
				
				intent.putExtra("mode", "Tutorial");
				
				intent.putExtra("levelXML", "BS");
				
                startActivity(intent);
			}
			
		});
		
		//displays level menu
		levelSelectButton.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				Intent l = new Intent(v.getContext(), LevelMenu.class);
                startActivity(l);
			}
			
		});
		
		//displays HighScore when clicked
		highScoreButton.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				Intent i = new Intent(v.getContext(), HighScoreView.class);
                startActivity(i);
			}
			
		});
				
		//displays Options menu when clicked
		creditsButton.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				Intent i = new Intent(v.getContext(), Credits.class);
                startActivity(i);
			}
			
		});
		
		//displays home page, exits app
		exitButton.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				finish();
			}
		});
	}
	
	// TODO This isn't a very practical application of this.
	// TODO These dimensions may need to be recalculated on the fly, so we're stuck with existing strategy
	private void setScreenPreferences() {

		Display display = getWindowManager().getDefaultDisplay();
		
		SharedPreferences mPrefs =
				getSharedPreferences(SharedConstants.SCREEN_PREFS, MODE_PRIVATE);
		
		SharedPreferences.Editor mPrefsEditor = mPrefs.edit();

		mPrefsEditor.putInt(SharedConstants.SCREEN_WIDTH, display.getWidth());
		mPrefsEditor.putInt(SharedConstants.SCREEN_HEIGHT, display.getHeight());
//		mPrefsEditor.putInt("boardHeight", mBoardHeight);
//		mPrefsEditor.putInt("boardWidth", mBoardWidth);
//		mPrefsEditor.putInt("offsetHeight", mOffsetHeight);
//		mPrefsEditor.putInt("offsetWidth", mOffsetWidth);
//		mPrefsEditor.putInt("sidePanelWidth", mSidePanelWidth);
//		mPrefsEditor.putInt("tileSize", mTileSize);
//		mPrefsEditor.putInt("wallSize", mWallSize);
//		mPrefsEditor.putInt("sidePanelOffset", mSidePanelOffset);

		mPrefsEditor.commit();
	}

	private void setCalibrationPreferences() {
		
		SharedPreferences mPrefs =
				getSharedPreferences(SharedConstants.CALIBRATION_PREFS, MODE_PRIVATE);
		
		SharedPreferences.Editor mPrefsEditor = mPrefs.edit();
	
		mPrefsEditor.putBoolean(SharedConstants.MIRROR_Y, false);
		mPrefsEditor.putInt(SharedConstants.ROTATIONS_CLOCK, 0);
		
		mPrefsEditor.putFloat(SharedConstants.TARE_X, (float) 0.0);
		mPrefsEditor.putFloat(SharedConstants.TARE_Y, (float) 0.0);
		
		mPrefsEditor.commit();
	}
	
	private boolean isFirstLaunch() {
		
		SharedPreferences mPrefs = 
			getSharedPreferences(SharedConstants.CONFIGURATION_PREFS, MODE_PRIVATE);
			
		SharedPreferences.Editor mPrefsEditor = mPrefs.edit();
		
		if(mPrefs.contains(SharedConstants.FIRST_LAUNCH)) {
			return mPrefs.getBoolean(SharedConstants.FIRST_LAUNCH, false);
		}
		else {
			mPrefsEditor.putBoolean(SharedConstants.FIRST_LAUNCH, false);
			mPrefsEditor.commit();
			return true;
		}
	}
	
}

